package lhy.autoparts.servlet;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lhy.autoparts.pojo.User;
import lhy.frontend.FrontendUtils;
import lhy.orm.dao.GenericDaoImpl;

import java.util.List;
import java.util.Map;
import java.util.Objects;


@WebServlet("/LoginServlet")
public class LoginServlet extends Utf8Servlet{
    @Override
    protected void doPost(HttpServletRequest que, HttpServletResponse pon) {
        super.doPost(que, pon);
        Map<String, Object> params = FrontendUtils.getParamMapFromAjax(que);
        String usr = (String) params.get("usr");
        String pwd = (String) params.get("pwd");
        List<User> users = new GenericDaoImpl<>(User.class).queryForAll();
        boolean success = users.stream().anyMatch(user -> Objects.equals(user.pwd, pwd) && Objects.equals(user.id, usr));
        write(pon,success);
    }
}
